<template>
  <div class="login-container">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px" class="login-form">
      <h2 class="login-title">在线考试</h2>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="loginMobile()">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          {required: true, message: "用户名不能为空", trigger: 'blur'},
          {min: 3, max: 10, message: "用户名3-5位", trigger: 'blur'}
        ],
        password: [
          {required: true, message: "密码不能为空", trigger: 'blur'},
          {min: 3, max: 10, message: "密码3-5位", trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    loginMobile() {
      const _this = this;
      // _this.GLOBAL.isDisable=true;
      // setTimeout(() => {
      //   _this.GLOBAL.isDisable=false;
      // }, 2500)
      _this.isDisable=true;
      setTimeout(() => {
        _this.isDisable=false;
      }, 2500)
      if (_this.form.username != "" && _this.form.password != "") {
        axios({
          method: "post",
          url: _this.APIurl.apiUrl.domainName+"user/login",
          headers: {
            "Content-Type": "application/json",
          },
          data:{
            username: _this.form.username,
            password: _this.form.password
          }
        })
            .then(function(resp) {
              switch(resp.data.code){
                case 1:
                  sessionStorage.setItem('id',resp.data.data.id);
                  sessionStorage.setItem('user', JSON.stringify(resp.data.data));
                  let redirect=_this.$route.query.redirect;
                  _this.$router.push({path:redirect});
                  _this.$message({
                    showClose: true,
                    message: '登录成功',
                    type: 'success'
                  });


                  break;
                case 0:
                  _this.usernameError = true;
                  break;
                case -1:
                  _this.passwordError = true;
                  break;
              }
            })
            .catch(function(error) {
              console.log(error);
            });

      }
    },
  }
};
</script>

<style acoped>
.login-form {
  width: 300px;
  margin: 160px auto; /* 上下间距160px，左右自动居中*/
  background-color:aliceblue; /* 透明背景色 */
  padding: 30px;
  border-radius: 20px; /* 圆角 */
}

/* 背景 */
.login-container {
  position: absolute;
  width: 60%;
  height: 100%;
}

/* 标题 */
.login-title {
  color: #303133;
  text-align: center;
}
</style> -->
